<?php

namespace App\Services;

use App\Repositories\Contracts\SystemModuleInfoRepository;

class SystemModuleInfoService
{
    private $systemModuleInfoRepository;

    public function __construct(SystemModuleInfoRepository $systemModuleInfoRepository)
    {
        $this->systemModuleInfoRepository = $systemModuleInfoRepository;
    }

    /**
     * 获取列表
     *
     * @param array|null $param
     */
    public function getAll($param = null)
    {
        try {
            $filter = [
                'module' => $param['module'],
            ];
            $data = $this->systemModuleInfoRepository->getAll($filter);
           
            $result['data'] = array_key_reversal($data['data']);
            
            return $result;
        } catch (\Throwable $th) {
            throw $th;
        }
    }

    /**
     * 新增
     *
     * @param array $param
     */
    public function add($param)
    {
        try {
            if (is_array($param['value']))
            {
                // 数组转json
                $param['value'] = json_encode($param['value']);
            }

            $systemModuleInfo = $this->systemModuleInfoRepository->store($param);
            return $systemModuleInfo;
        } catch (\Throwable $th) {
            throw $th;
        }
    }

    /**
     * 修改
     *
     * @param array $param
     * @param int $id
     */
    public function update($param, $id)
    {
        try {
            $systemModuleInfo = $this->systemModuleInfoRepository->update($param, $id);
            return $systemModuleInfo;
        } catch (\Throwable $th) {
            throw $th;
        }
    }

    /**
     * 删除
     *
     * @param int $id
     */
    public function delete($id)
    {
        try {
            $systemModuleInfo = $this->systemModuleInfoRepository->delete($id);

            $result = [];
            if ($systemModuleInfo) {
                $result = 'success';
            } else {
                $result = 'error';
            }
            return $result;

        } catch (\Throwable $th) {
            throw $th;
        }
    }
}
